package com.kh.pajx.sop.service.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.UrlPathHelper;

import com.kh.pajx.sop.base.BaseServiceImpl;
import com.kh.pajx.sop.dao.ISysLogDao;
import com.kh.pajx.sop.domain.SysLog;
import com.kh.pajx.sop.service.ISysLogService;
import com.kh.pajx.sop.util.common.FormatDate;
import com.kh.pajx.sop.util.web.RequestUtils;

/**
 * 日志信息业务逻辑实现类
 * @author:	azzcsimp
 * @Createdate:	2014年7月3日 下午5:57:51
 */
@Service
@Transactional
public class SysLogServiceImpl extends BaseServiceImpl<SysLog> implements ISysLogService {
	
	@Resource
	private ISysLogDao sysLogDao;
	
	/**
	 * 记录登录失败日志
	 * @see com.kh.pajx.sop.service.ISysLogService#loginFailure(javax.servlet.http.HttpServletRequest, java.lang.String)
	 */
	@Override
	public void loginFailure(HttpServletRequest request,String content) {
		UrlPathHelper helper = new UrlPathHelper();
		String uri = helper.getOriginatingRequestUri(request);
		SysLog log = new SysLog();
		log.setSysNameDc("SOP"); // 系统名称字典编码
		log.setLogType(1); // 日志类型
		log.setLogMsg(content); //日志内容
		log.setLogTime(FormatDate.dateTimeDetail()); // 日志记录时间
		log.setLogSendType(0);  // 日志推送类型
		//log.setLogSendUserId(); // 日志推送对象ID
		//log.setLogReadTime(logReadTime); // 日志查看时间
		log.setLogSendIp(RequestUtils.getIpAddr(request)); // 产生日志IP
		log.setLogSendUrl(uri); // 产生日志url
		log.setLogTitle("登录成功");
		sysLogDao.save(log);
	}
	
	/*
	public CmsLog loginFailure(HttpServletRequest request, String title,
			String content) {
		String ip = RequestUtils.getIpAddr(request);
		UrlPathHelper helper = new UrlPathHelper();
		String uri = helper.getOriginatingRequestUri(request);
		Date date = new Date();
		CmsLog log = save(CmsLog.LOGIN_FAILURE, null, null, uri, ip, date,
				MessageResolver.getMessage(request, title), content);
		return log;
	}
	*/
	
/*	@Transactional(readOnly = true)
	public Pagination getPage(Integer category, Integer siteId,
			String username, String title, String ip, int pageNo, int pageSize) {
		Pagination page;
		if (StringUtils.isBlank(username)) {
			page = dao.getPage(category, siteId, null, title, ip, pageNo,
					pageSize);
		} else {
			CmsUser user = cmsUserMng.findByUsername(username);
			if (user != null) {
				page = dao.getPage(category, siteId, user.getId(), title, ip,
						pageNo, pageSize);
			} else {
				page = new Pagination(1, pageSize, 0, new ArrayList<CmsLog>(0));
			}
		}
		return page;
	}

	@Transactional(readOnly = true)
	public CmsLog findById(Integer id) {
		CmsLog entity = dao.findById(id);
		return entity;
	}

	public CmsLog save(Integer category, CmsSite site, CmsUser user,
			String url, String ip, Date date, String title, String content) {
		CmsLog log = new CmsLog();
		log.setSite(site);
		log.setUser(user);
		log.setCategory(category);
		log.setIp(ip);
		log.setTime(date);
		log.setUrl(url);
		log.setTitle(title);
		log.setContent(content);
		save(log);
		return log;
	}

	public CmsLog loginSuccess(HttpServletRequest request, CmsUser user,
			String title) {
		String ip = RequestUtils.getIpAddr(request);
		UrlPathHelper helper = new UrlPathHelper();
		String uri = helper.getOriginatingRequestUri(request);
		Date date = new Date();
		CmsLog log = save(CmsLog.LOGIN_SUCCESS, null, user, uri, ip, date,
				MessageResolver.getMessage(request, title), null);
		return log;
	}

	public CmsLog loginFailure(HttpServletRequest request, String title,
			String content) {
		String ip = RequestUtils.getIpAddr(request);
		UrlPathHelper helper = new UrlPathHelper();
		String uri = helper.getOriginatingRequestUri(request);
		Date date = new Date();
		CmsLog log = save(CmsLog.LOGIN_FAILURE, null, null, uri, ip, date,
				MessageResolver.getMessage(request, title), content);
		return log;
	}

	public CmsLog operating(HttpServletRequest request, String title,
			String content) {
		CmsSite site = CmsUtils.getSite(request);
		CmsUser user = CmsUtils.getUser(request);
		String ip = RequestUtils.getIpAddr(request);
		UrlPathHelper helper = new UrlPathHelper();
		String uri = helper.getOriginatingRequestUri(request);
		Date date = new Date();
		CmsLog log = save(CmsLog.OPERATING, site, user, uri, ip, date,
				MessageResolver.getMessage(request, title), content);
		return log;
	}

	public CmsLog save(CmsLog bean) {
		dao.save(bean);
		return bean;
	}

	public int deleteBatch(Integer category, Integer siteId, Integer days) {
		Date date = null;
		if (days != null && days > 0) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DAY_OF_MONTH, -days);
			date = cal.getTime();
		}
		return dao.deleteBatch(category, siteId, date);
	}

	public CmsLog deleteById(Integer id) {
		CmsLog bean = dao.deleteById(id);
		return bean;
	}

	public CmsLog[] deleteByIds(Integer[] ids) {
		CmsLog[] beans = new CmsLog[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private CmsUserMng cmsUserMng;
	private CmsLogDao dao;

	@Autowired
	public void setCmsUserMng(CmsUserMng cmsUserMng) {
		this.cmsUserMng = cmsUserMng;
	}

	@Autowired
	public void setDao(CmsLogDao dao) {
		this.dao = dao;
	}*/
}
